import 'dart:io';

import 'package:device_info_plus/device_info_plus.dart';

class DeviceInfoUtils {
  /// 设备信息
  AndroidDeviceInfo? _androidInfo;
  IosDeviceInfo? _iosInfo;

  /// 工厂模式
  factory DeviceInfoUtils() => _getInstance();

  /// 单例
  static DeviceInfoUtils get instance => _getInstance();
  static DeviceInfoUtils? _instance;

  DeviceInfoUtils._internal();

  static DeviceInfoUtils _getInstance() => _instance ??= DeviceInfoUtils._internal();

  /// 获取Android设备信息
  Future<AndroidDeviceInfo> get getAndroidDeviceInfo async => _androidInfo ??= await DeviceInfoPlugin().androidInfo;

  /// 获取ios设备信息
  Future<IosDeviceInfo> get getIosDeviceInfo async => _iosInfo ??= await DeviceInfoPlugin().iosInfo;

  /// 获取deviceId
  Future<String?> get getDeviceId async {
    if (Platform.isIOS) {
      final IosDeviceInfo? iosInfo = await getIosDeviceInfo;
      return iosInfo!.identifierForVendor;
    } else if (Platform.isAndroid) {
      /// Todo Android不在提供获取设备唯一id的方法,以前是用的fb获取，现在是使用插件
      final AndroidDeviceInfo? androidInfo = await getAndroidDeviceInfo;
      return androidInfo!.androidId;
    } else {
      return "";
    }
  }
}
